package com.xxxx.server.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.xxxx.server.pojo.*;
import com.xxxx.server.service.*;
import com.xxxx.server.util.RespBean;
import com.xxxx.server.util.RespPageBean;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URL;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wyd
 * @since 2023-06-11
 */
@RestController
@RequestMapping("/employee/basic")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IPoliticsStatusService politicsStatusService;
    @Autowired
    private IJoblevelService joblevelService;
    @Autowired
    private INationService nationService;
    @Autowired
    private IPositionService positionService;
    @Autowired
    private IDepartmentService departmentService;

    @ApiOperation(value = "分页获取所有员工")
    @GetMapping("/")
    public RespPageBean getEmployee(@RequestParam(defaultValue = "1")Integer currentPage,
                                    @RequestParam(defaultValue = "10") Integer pageSize,
                                    Employee employee,
                                    LocalDate[] beginDateScope){

        return employeeService.getEmpByPage(currentPage,pageSize,employee,beginDateScope);

    }

    @ApiOperation(value = "获取所有政治面貌")
    @GetMapping("/politicsStatus")
    public List<PoliticsStatus> getAllPoliticsStatus(){

        return politicsStatusService.list();
    }

    @ApiOperation(value = "获取所有职称")
    @GetMapping("/jobLevels")
    public List<Joblevel> getAlljobLevels(){

        return joblevelService.list();
    }

    @ApiOperation(value = "获取所有职称民族")
    @GetMapping("/nations")
    public List<Nation> getAllNations(){

        return nationService.list();
    }

    @ApiOperation(value = "获取所有职位")
    @GetMapping("/positions")
    public List<Position> getAllPositions(){

        return positionService.list();
    }

    @ApiOperation(value = "获取所有部门")
    @GetMapping("/deps")
    public List<Department> getAllDepartments(){

        return departmentService.list();
    }

    @ApiOperation(value = "获取工号")
    @GetMapping("/maxWorkId")
    public RespBean maxWorkedId(){

        return employeeService.maxWorkedId();
    }

    @ApiOperation(value = "添加员工")
    @PostMapping("/")
    public RespBean addEmp(@RequestBody Employee employee){

        return employeeService.addEmp(employee);
    }

    @ApiOperation(value = "更新员工")
    @PutMapping("/")
    public RespBean updateEmp(@RequestBody Employee employee){

        if (employeeService.updateById(employee)){

            return RespBean.success("更新员工成功!");
        }

        return RespBean.error("更新员工失败!");
    }

    @ApiOperation(value = "删除员工")
    @DeleteMapping("/{id}")
    public RespBean deleteEmp(@PathVariable("id") Integer empId){

        if (employeeService.removeById(empId)){

            return RespBean.success("删除员工成功!");
        }

        return RespBean.error("删除员工失败!");
    }

    @ApiOperation(value = "导出员工数据")
    @GetMapping(value = "/export",produces = "application/octet-stream")
    public void exportEmployeeExcel(HttpServletResponse response){

        List<Employee> employeeExcel = employeeService.getEmployeeExcel(null);

        ExportParams params = new ExportParams("员工表", "员工表", ExcelType.HSSF);

        Workbook workbook = ExcelExportUtil.exportExcel(params, Employee.class, employeeExcel);

        ServletOutputStream outputStream = null;

        try {

            //用流的形式传输
            response.setHeader("content-type","application/octet-stream");

            //防止中文乱码
            response.setHeader("content-disposition","attachment;filename="+ URLEncoder.encode("员工表.xls","UTF-8"));

           outputStream =  response.getOutputStream();

           workbook.write(outputStream);

        }catch (IOException e){

            e.printStackTrace();
        }finally {

            //关闭流
            if (null != outputStream){


                try {
                    outputStream.close();

                }catch (IOException e){

                    e.printStackTrace();
                }
            }
        }
    }
}
